#pragma once
#include <iostream>
#include <string>

class Task
{
public:
    Task(int sockfd, const std::string &ip, const uint16_t &port)
        : _sockfd(sockfd), _clientip(ip), _clientport(port)
    {
    }
    
    //一个线程只调用一次
    void Run()
    {
        char buf[4096];
        ssize_t n = read(_sockfd, buf, sizeof(buf));
        if (n > 0)
        {
            buf[n] = 0;
            std::cout << "client say# " << buf << std::endl;
            std::string echo_string = "tcpserver say#";
            echo_string += buf;
            write(_sockfd, echo_string.c_str(), echo_string.size());
        }
        else if (n == 0) // 客户端退出
        {
            printf("%s:%d quit,server close sockfd: %d\n", _clientip.c_str(), _clientport, _sockfd);
        }
        else
        {
            printf("read error,sockfd: %d,client ip: %s,client port: %d", _sockfd, _clientip.c_str(), _clientport);
        }
    }
    void operator()() // 仿函数
    {
        Run();
    }

    ~Task()
    {
    }

private:
    int _sockfd;
    std::string _clientip;
    uint16_t _clientport;
};